package com.istock.user.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.istock.base.common.api.model.ApiBaseResponse;
import com.istock.base.common.api.model.PageInfo;
import com.istock.base.common.api.model.RetStatusEnum;
import com.istock.union.user.client.SSOClientUtils;
import com.istock.union.user.model.Constants;
import com.istock.union.user.model.OperLog;
import com.istock.union.user.model.OrderByModel;
import com.istock.union.user.model.SSOUser;
import com.istock.union.user.model.SecurityChecker;
import com.istock.union.user.model.UserOnlineInfo;
import com.istock.union.user.security.InnerTokenService;
import com.istock.union.user.service.UserOnlineInfoService;

@Controller
@RequestMapping("/s/onlineInfo")
public class UserOnlineController {

	@Autowired
	private UserOnlineInfoService onlineService;
	
	@Autowired
	private InnerTokenService tokenService;
	
	@RequestMapping("showList")
	@SecurityChecker("user:onlineInfo:search")
	public String showList(){
		return "user-app/userOnline/list";
	}
	
	@RequestMapping("search")
	@SecurityChecker("user:onlineInfo:search")
	@OperLog(functionName="查询" , menuName ="在线用户")
	public @ResponseBody ApiBaseResponse<PageInfo<UserOnlineInfo>> search(String loginName, String ipaddr, PageInfo<UserOnlineInfo> page,OrderByModel orderBy){
		if(page == null){
			page = new PageInfo<UserOnlineInfo>();
		}
		List<String> deptIdList = new ArrayList<String>();
		if(SSOClientUtils.getInstance().isAdmin()) {
			//如果是管理员,能看到所有的登录信息
		}else {
			//只能看到当前登录机构的用户
			SSOUser userInfo = SSOClientUtils.getInstance().findCurrentUser();
			if(userInfo.getCurrentDept() != null) {
				deptIdList.add(userInfo.getCurrentDept().getId());
			}
			
		}
		onlineService.search(loginName, deptIdList, ipaddr, page , orderBy);
		return new ApiBaseResponse<PageInfo<UserOnlineInfo>>(RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE, 
				Constants.API_RESPONSE_SUCCESS_MESSAGE, page);
	}
	
	@RequestMapping("forceLogout")
	@OperLog(functionName="强制退出",menuName="在线用户")
	@SecurityChecker("user:onlineInfo:forceLogout")
	public @ResponseBody ApiBaseResponse<Boolean> forceLogout(String[] ids){
		if(ids != null && ids.length>0){
			for(String token : ids){
				tokenService.invalidToken(token);
			}
			onlineService.remveUserOnlineInfo(ids);
		}
		return new ApiBaseResponse<Boolean>( RetStatusEnum.SUCCESS,Constants.API_RESPONSE_SUCCESS_CODE,
				Constants.API_RESPONSE_SUCCESS_MESSAGE, true);
	}
	
	@RequestMapping("batchForceLogout")
	@OperLog(functionName="批量强制退出",menuName="在线用户")
	@SecurityChecker("user:onlineInfo:batchForceLogout")
	public @ResponseBody ApiBaseResponse<Boolean> batchForceLogout(@RequestParam("ids[]") String[] ids){
		return forceLogout(ids);
	}
}
